package com.lihh.demo;

import java.util.concurrent.*;

public class ThenCombineTest001
{
    public static void main(String[] args) throws ExecutionException, InterruptedException
    {
        ExecutorService executors = Executors.newSingleThreadExecutor();

        CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("这是执行A");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "a";
        }, executors);

        CompletableFuture<String> strFuture = uCompletableFuture.thenCombineAsync(CompletableFuture.supplyAsync(() -> {
            System.out.println("这是执行B");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "b";
        }, executors), (res1, res2) -> {
            System.out.println("执行计划A AND B之后，开始执行此方法");
            return res2 + res1;
        });

        String result = strFuture.get();
        System.out.println(result);
    }
}
